import sys
import pickle
# USERINFO=r'D:\AK\登录注册\stuinfo'
# STUINFO=r'D:\AK\登录注册\userinfo'
class Course(object):
    def __init__(self,name,price,period):
        self.name=name
        self.price=price
        self.period=period


#创建学生
#   让用户输入用户名，密码
#   实例化对象
#   把用户名，密码写到userinfo
#   吧学生对象写到stuinfo文件里
class Student(object):
    opt_lst = [
               ('查看课程', 'show_courses'), ('选择课程', 'choose_course'),
               ('查看已选课程', 'show_selected'), ('退出', 'exit')]

    def __init__(self,name):
        self.name=name

    def show_courses(self):
        pass

    def choose_course(self):
        pass

    def show_selected(self):
        pass

    def exit(self):
        pass


class Manager(object):
    opt_lst=[('创建课程','create_course'),('创建学生','create_student'),
             ('查看课程','show_courses'),('查看学生','show_students'),
             ('查看学生和已选课程','show_stu_cource'),('退出','exit')]
    def __init__(self,name):
        self.name=name
        self.courses=[]
    def create_course(self):
        pass

    def show_courses(self):
        pass

    def create_student(self):
        print('创建学生')
        #输入用户名，密码
        user=input('username :')
        pwd='123456'#默认密码
        stu=Student(user)
        with open('userinfo',encoding='utf-8',mode='a') as f:
            f.write('%s|123456|Student\n'%(user))
        with open('stuinfo',mode='ab') as f:
            pickle.dump(stu,f)
        print('%s学生创建成功'%stu.name)#这里可以写日志

    def show_students(self):
        pass

    def show_stu_cource(self):
        pass

    def exit(self):
        pass


#用户 输入用户名 密码 判断用户是否合法和身份是啥
def login():
    '''

    return : 登陆成功：用户名，身份，登录不成功： return False
    '''

    username=input('username：')
    password = input('password：')
    with open('userinfo',encoding='utf-8') as f:
        for line in f:
            user,pwd,ident=line.strip().split('|')
            if user==username and password==pwd:
                return user,ident
        else:
            return False
ret=login()
if ret:
    print('登陆成功,%s,欢迎使用选课系统'%ret[0])
    cls=getattr(sys.modules[__name__],ret[1])
    print(cls,Manager,Student)
    if ret[1]=='Manager':
        obj=Manager(ret[0])
    elif ret[1]=='Student':
        with open('stuinfo','rb') as f:
            while True:
                try:
                    obj=pickle.load(f)
                    if obj.name==ret[0]:
                        break
                except EOFError as e:
                    print(e,'没有这个学生，出现了不可思议的错误')
    for index,opt in enumerate(cls.opt_lst,1):
        print(index,opt[0])
    num=int(input('你要选择的操作 :').strip())
    if hasattr(obj,cls.opt_lst[num-1][1]):
        getattr(obj,cls.opt_lst[num-1][1])()
else:
    print('登陆失败')
























































